package com.example.myapplication.frament;

import android.content.Context;
import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import androidx.viewpager.widget.ViewPager;


import com.example.myapplication.BaseFragment;
import com.example.myapplication.OnItemClickListener;
import com.example.myapplication.R;
import com.example.myapplication.adapter.ImageTitlePagerAdapter;

import com.example.myapplication.homesosuo.ICallBack;
import com.example.myapplication.homesosuo.RecordSQLiteOpenHelper;
import com.example.myapplication.homesosuo.SearchListView;

import com.example.myapplication.homesosuo.bCallBack;
import com.example.myapplication.okhttp.HttpUtil;



import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class FragmentHome extends BaseFragment {
    private List viewList;
    private ImageView ivImage;
    private Context context;
 TextView tempTv,cityTv,conditonTv,windTv,tempRangeTv,dataeTv;
 ImageView dayIv;



    private ViewPager viewPager;


    // ListView列表 & 适配器
    private SearchListView listView;
    private BaseAdapter adapter;

    // 数据库变量
    // 用于存放历史搜索记录
    private RecordSQLiteOpenHelper helper ;
    private SQLiteDatabase db;
    // 回调接口
    private ICallBack mCallBack;// 搜索按键回调接口
    private  bCallBack bCallBack; // 返回按键回调接口

    // 自定义属性设置
    // 1. 搜索字体属性设置：大小、颜色 & 默认提示
    private Float textSizeSearch;
    private int textColorSearch;
    private String textHintSearch;

    // 2. 搜索框设置：高度 & 颜色
    private int searchBlockHeight;
    private int searchBlockColor;
    private TextView llDot;
    private SparseBooleanArray isLarge;
    private int index = 0;
    private OnItemClickListener onItemClickListener;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext() , R.layout.fragment_home , null);
        TextView textView = view.findViewById(R.id.tv_clear);
        initView(view);



        List list = new ArrayList();
        View view1 = View.inflate(getContext(), R.layout.carousel_item,null);
        View view2 = View.inflate(getContext(), R.layout.carousel_item1,null);
        View view3 = View.inflate(getContext(), R.layout.carousel_item2,null);
        list.add(view1);
        list.add(view2);
        list.add(view3);
        context = getContext();
        viewPager = view.findViewById(R.id.vp_image_title);
        viewPager.setAdapter(new ImageTitlePagerAdapter(list, new ImageTitlePagerAdapter.OnImageClickListener() {
            @Override
            public void onClick(int position) {

                Intent intent = new Intent(getContext() , com.example.myapplication.wangye.class);
                String value ="";
                switch (position){
                    case 0:
                        value="历史";
                        break;
                    case 1:
                        value = "民族";
                        break;
                    case 2:
                        value = "热点";
                        break;
                }
                intent.putExtra("key",value);

                getActivity().startActivity(intent);
            }
        }));

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        viewPager.setCurrentItem(index);
                        index++;
                        index %=3;
                    }
                });
            }
        },0,5000);

        ViewPager mViewPager = view.findViewById(R.id.vp_image_title);

        context = getContext();
        getResult();
        return view;

    }

    private void getResult() {
        new Thread(new Runnable() {  //子线程
            @Override
            public void run() {
                try {
                    JSONObject weather = HttpUtil.getWeather("株洲");
                    System.out.println("weather ="+weather);
                    JSONObject result1 = weather.getJSONObject("result");
                    JSONObject realtime = result1.getJSONObject("realtime");
                    String city = result1.getString("city");
                    String info = realtime.getString("info");
                    String direct = realtime.getString("direct");
                    String power = realtime.getString("power");
                    String temperature = realtime.getString("temperature");
//                    System.out.println("result=="+result1.toString());
                    JSONArray jsonArray =result1.getJSONArray("future");
//                    System.out.println("jsonarray = "+jsonArray);
                    JSONObject temp = jsonArray.getJSONObject(0);

                    String date = temp.getString("date");

                    String  temper= temp.getString("temperature");

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            cityTv.setText(city);
                            tempTv.setText(temperature);
                            windTv.setText(direct+power);
                            conditonTv.setText(info);
                            dataeTv.setText(date);
                            tempRangeTv.setText(temper);
                            if (info .equals("晴")){
                                        dayIv.setImageResource(R.drawable.qing);
                                    }else if(info .equals("阴") ){
                                        dayIv.setImageResource(R.drawable.yin);
                                        System.out.println("dayIv="+dayIv);
                                    }else if (info .equals("多云")||info .equals("小雨转多云")){
                                        dayIv.setImageResource(R.drawable.duoyun);
                                    }else if (info .equals("大雨")){
                                        dayIv.setImageResource(R.drawable.dayu);
                                    }else {
                                        dayIv.setImageResource(R.drawable.xiaoyu);

                               }
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView(View view) {
        //用于初始化
        tempTv =view.findViewById(R.id.frag_tv_currenttemp);
        cityTv =view.findViewById(R.id.frag_tv_city);
        conditonTv =view.findViewById(R.id.frag_tv_condition);
        windTv =view.findViewById(R.id.frag_tv_wind);
        tempRangeTv =view.findViewById(R.id.frag_tv_temprange);
        dataeTv =view.findViewById(R.id.frag_tv_date);
         dayIv=view.findViewById(R.id.frag_tv_today);

    }



    public void setOnItemClickListener(OnItemClickListener listener){
        onItemClickListener = listener;
}



}
